<?php

use \qore\util\collections\Configuration;
use \qore\error\Exception;

/**
 * Gets the application start timestamp.
 */
defined('QORE_BEGIN_TIME') or define('QORE_BEGIN_TIME', microtime(true));
/**
 * This constant defines whether exception handling should be enabled. Defaults to true.
 */
defined('QORE_ENABLE_EXCEPTION_HANDLER') or define('QORE_ENABLE_EXCEPTION_HANDLER', true);
/**
 * This constant defines whether error handling should be enabled. Defaults to true.
 */
defined('QORE_ENABLE_ERROR_HANDLER') or define('QORE_ENABLE_ERROR_HANDLER', true);

defined('QORE_BASE_PATH') or define('QORE_BASE_PATH', dirname(__FILE__));

/**
 * QoreBase is a helper class serving common framework functionalities.
 *
 * Do not use QoreBase directly. Instead, use its child class {@link Qore} where
 * you can customize methods of QoreBase.
 *
 * @author Samuel Nogueira <samuel.nogueira.dev@gmail.com>
 */
class QoreBase {
	
	private static $app;
	private static $autoloader;

	/**
	 * Creates a Web application instance.
	 * @param mixed $config application configuration.
	 * If a string, it is treated as the path of the file that contains the configuration;
	 * If an array, it is the actual configuration information.
	 * @return \qore\web\WebApplication
	 */
	public static function createWebApplication($config = null) {
		return static::createApplication('\qore\web\WebApplication', $config);
	}

	/**
	 * Creates a Cli application instance.
	 * @param mixed $config application configuration.
	 * If a string, it is treated as the path of the file that contains the configuration;
	 * If an array, it is the actual configuration information.
	 * @return \qore\cli\CliApplication
	 */
	public static function createCliApplication($config = null) {
		return static::createApplication('\qore\cli\CliApplication', $config);
	}

	public static function app() {
		return self::$app;
	}
	
	public static function createApplication($class, $config = null) {
		$autoloaderClass = get_class(static::autoloader());
		spl_autoload_unregister("$autoloaderClass::autoload");
		spl_autoload_register("$autoloaderClass::autoload");
		
		return self::$app = new $class($config);
	}
	
	public static function autoloader() {
		if (isset(self::$autoloader)) return self::$autoloader;
		
		require_once(QORE_BASE_PATH.'/AutoLoader.php');
		return new AutoLoader();
	}

	/**
	 * Translates a message to the specified language.
	 * @param string $context Context of the message
	 * @param string $message The original message
	 * @param array $params Parameters to be applied to the message
	 * @param string $language The target language. If null (default), the {@link CApplication::getLanguage application language} will be used.
	 * @return string The translated message
	 */
	public static function t($context, $message, $params = array(), $language = null) {
		// $t_language = $language ?: static::app()->getLanguage();
        // TODO: Actually do translation
        return vsprintf($message, $params);
	}
	
	/**
	 * Returns the value from the given array if it exists, defaultValue otherwise
	 * 
	 * @param array $array
	 * @param mixed $key If its an array, will test for every element
	 * @param mixed $defaultValue Value to be returned if key does not exist
	 * @return mixed
	 */
	public static function vget(array $array, $key, $defaultValue = null) {
		// array behaviour
		if (is_array($key)) {
			foreach ($key as $keyValue) {
				if (isset($array[$keyValue]) || array_key_exists($keyValue, $array)) return $array[$keyValue];
			}
			
			return $defaultValue;
		}
		
		// normal behaviour
		return isset($array[$key]) || array_key_exists($key, $array) ? $array[$key] : $defaultValue;
	}
	
	/**
	 * Remove indentation from given string
	 * 
	 * @return string
	 */
	public static function removeIndent($string) {
		return preg_replace('/^\t+/m', '', $string);
	}
}
